package com.wangyi.fightimg.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXEmojiObject;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.wangyi.fightimg.R;
import com.wangyi.fightimg.moduls.MainActivity;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * @author wangYi
 *         分享工具类
 */

public class ShareUtil {

    /**
     * 分享到QQ
     *
     * @param imgUrl
     */
    public static void shareToQQ(Context context, String imgUrl, IUiListener iUiListener) {
        Tencent mTencent = Tencent.createInstance(Constants.TECENT_APP_ID, context);
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, imgUrl);
        mTencent.shareToQQ((Activity) context, params, iUiListener);
    }

    /**
     * 分享到QQ空间
     *
     * @param imgUrl
     */

    public static void shareToQZone(Context context, String imgUrl, IUiListener iUiListener) {
        Tencent mTencent = Tencent.createInstance(Constants.TECENT_APP_ID, context);
        Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, imgUrl);
        params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN); //打开这句话，可以实现分享纯图到QQ空间
        mTencent.shareToQQ((MainActivity) context, params, iUiListener);
    }

    /**
     * 分享到微信朋友圈,不支持gif图片，发送的gif图片为静态图片
     *
     * @param imgUrl
     */
    public static void shareToWechatMoments(IWXAPI api, String imgUrl) {
        Bitmap bmp = BitmapFactory.decodeFile(imgUrl);
        WXImageObject imgObj = new WXImageObject(bmp);

        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 120, 120, true);
        bmp.recycle();
        msg.thumbData = bmpToByteArray(thumbBmp, true);

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    /**
     * 分享到微信好友
     *
     * @param api
     * @param imgUrl 分享图片地址
     */
    public static void shareToWechat(IWXAPI api, String imgUrl) {
        Bitmap bitmap = BitmapFactory.decodeFile(imgUrl);

        //创建一个WXWebPageObject对象，用于封装要发送的Url
        WXEmojiObject imgObj = new WXEmojiObject();
        imgObj.emojiPath = imgUrl;
        //创建一个WXMediaMessage对象
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = imgObj;

        Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, true);
        msg.thumbData = bmpToByteArray(mBitmap, true);
        mBitmap.recycle();

        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("img");///transaction字段用于唯一标识一个请求，这个必须有，否则会出错
        req.message = msg;

        //表示发送给朋友圈  WXSceneTimeline  表示发送给朋友  WXSceneSession
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }


    /**
     * 分享到新浪微博
     *
     * @param imgUrl
     */
    public static void shareToSinaweibo(String imgUrl) {
        Platform.ShareParams sp = new Platform.ShareParams();
        sp.setText("测试分享的文本");
        sp.setImagePath(imgUrl);

        Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
        weibo.setPlatformActionListener(new PlatformActionListener() {
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                //失败的回调，arg:平台对象，arg1:表示当前的动作，arg2:异常信息
                Log.e("weichat", "Platform-->>" + arg0.toString() + "Throwable-->>" + arg2.toString());
            }

            public void onComplete(Platform arg0, int arg1, HashMap<String, Object> arg2) {
                //分享成功的回调
                Log.e("weichat", "onComplete:" + arg0.toString());
            }

            public void onCancel(Platform arg0, int arg1) {
                //取消分享的回调
                Log.e("weichat", "onCancel:" + arg0.toString());
            }
        }); // 设置分享事件回调
        // 执行图文分享
        weibo.share(sp);
    }

    /**
     * 腾讯分享回调监听
     */
    public static IUiListener initViews(final Context context) {
        return new IUiListener() {
            @Override
            public void onComplete(Object o) {
                //分享成功后回调
                Toast.makeText(context, R.string.share_faile, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(UiError uiError) {
                //分享失败后回调
                Toast.makeText(context, R.string.share_faile, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel() {
                //取消分享后回调
                Toast.makeText(context, R.string.share_faile, Toast.LENGTH_SHORT).show();
            }
        };
    }
}
